<template>
  <div>
    <!-- 面包屑导航区域 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品分类</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 卡片区域 -->
    <el-card>
      <!-- 添加角色按钮区域 -->
      <el-button type="warning" plain @click="showAddCate">添加分类</el-button>

      <!-- 渲染分类区域 -->
      <!-- data表示数据的来源 -->
      <!-- :selection-type="false"关闭默认有复选框盒子 -->
      <tree-table
        :data="cateList"
        :columns="columns"
        border
        :show-index="true"
        :expand-type="false"
        index-text="#"
        :selection-type="false"
      >
        <template slot-scope="scope" slot="isOk">
          <i style="color:lightgreen" class="el-icon-success" v-if="scope.row.cat_deleted===false"></i>
          <i style="color:red" class="el-icon-error" v-else></i>
        </template>
        <template slot-scope="scope" slot="order">
          <el-tag type="success" v-if="scope.row.cat_id===1">一级</el-tag>
          <el-tag type="warning" v-else-if="scope.row.cat_id===2">二级</el-tag>
          <el-tag type="danger" v-else>三级</el-tag>
        </template>
        <template slot="option" slot-scope="scope">
          <el-button
            plain
            size="mini"
            type="warning"
            icon="el-icon-edit"
            @click="edtCate(scope.row)"
          >编辑</el-button>
          <el-button plain size="mini" type="danger " icon="el-icon-delete">删除</el-button>
        </template>
      </tree-table>

      <!-- 分页区域 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-size="queryInfo.pagesize"
        :page-sizes="[10, 15, 30, 40]"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      ></el-pagination>
    </el-card>

    <!-- 添加分类的弹窗 -->
    <el-dialog title="提示" :visible.sync="addCategoryDialogVisible" width="30%" @close="closeDialog">
      <el-form :model="addCate" :rules="addCateRule" ref="addCateRef" label-width="100px">
        <el-form-item label="分类名称" prop="cat_name">
          <el-input v-model="addCate.cat_name"></el-input>
        </el-form-item>
        <el-form-item label="父级分类">
          <el-cascader
            v-model="selectedKeys"
            :options="parentKeys"
            :props="cascaderProps"
            @change="handleChange"
          ></el-cascader>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="addCategoryDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="addCateHandle">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: 'category',
  data() {
    return {
      cascaderProps: {
        value: 'cat_id',
        label: 'cat_name',
        children: 'children',
        checkStrictly: true, // 表示多选
        expandTrigger: 'hover'
      },
      // 级联选择器选择的值
      selectedKeys: [],
      // 为级联选择器提供的数据(所有数据)
      parentKeys: [],
      // 控制添加分类弹窗的可见性
      addCategoryDialogVisible: false,
      // 获取全部数据的请求参数
      queryInfo: {
        type: 3,
        pagenum: 1,
        pagesize: 10
      },
      // 所有的数据列表数组
      cateList: [],
      total: 0,
      // 树形结构展示的数据
      columns: [
        {
          label: '分类名称',
          prop: 'cat_name'
        },
        {
          label: '是否有效',
          type: 'template',
          template: 'isOk'
        },
        {
          lable: '排序',
          type: 'template',
          template: 'order'
        },
        {
          lable: '操作',
          type: 'template',
          template: 'option'
        }
      ],
      // 添加分类列表
      addCate: {
        cat_pid: '',
        cat_name: '',
        cat_level: ''
      },
      // 对于添加分类字段的验证规则
      addCateRule: {
        cat_name: [{ required: true, message: '请输入分类名称', trigger: 'blur' }]
      }
    }
  },
  methods: {
    // 关闭对话框触发的事件
    closeDialog() {
      this.$refs.addCateRef.resetFields()
      this.selectedKeys = []
      this.addCate.cat_pid = 0
    },

    addCateHandle() {
      // 处理确认分类添加的方法
      this.$refs.addCateRef.validate(async valid => {
        const { data: res } = await this.$http.post('categories', this.addCate)
        console.log(res)
      })
    },

    // 处理的是级联选择器改变
    handleChange() {
      // 判读级连选择器的长度，分为大于0 、和等于0两类
      if (this.selectedKeys.length > 0) {
        this.addCate.cat_pid = this.selectedKeys[this.selectedKeys.length - 1]
        this.addCate.cat_level = this.selectedKeys.length
      } else {
        this.addCate.cat_pid = 0
        this.addCate.cat_level = 0
      }
    },

    // 添加分类
    showAddCate() {
      this.addCates()
      this.addCategoryDialogVisible = true
    },

    async addCates() {
      const { data: res } = await this.$http.post('categories', {
        params: { type: 2 }
      })
      if (res.meta.status !== 200) {
        return this.$message.error(res.meta.msg)
      }

      console.log(res)
    },

    // 分页区域每页显示条数改变
    handleSizeChange(newSize) {
      this.queryInfo.pagesize = newSize
      this.getCateList()
    },
    // 分页区域页码值改变时
    handleCurrentChange(newNum) {
      this.queryInfo.pagenum = newNum
      this.getCateList()
    },

    // 获取所有的数据
    async getCateList() {
      const { data: res } = await this.$http.get('categories', this.queryInfo)
      if (res.meta.status !== 200) {
        return this.$message.error(res.meta.msg)
      }
      // this.parentKeys = res.data
      this.cateList = res.data
      this.total = res.data.length
    }
  },
  created() {
    this.getCateList()
  }
}
</script>

<style lang="less" scoped>
.zk-table {
  margin-top: 15px;
}
.el-cascader {
  width: 100%;
}
</style>
